<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-08-27 09:19:48
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 工序
 */

namespace App\Models;

use Illuminate\Support\Facades\App as AppCall;

class ProProcessesPass extends BaseModel
    {
    public $timestamps = true;
    public $table = "sys_pro_process_pass";
    protected $guarded = [];
    protected $casts = [];
    public function out_process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function open_card()
        {
        return $this->belongsTo(OpenCardDetails::class);
        }
    public function device()
        {
        return $this->belongsTo(Loom::class);
        }
    public function direction()
        {
        return $this->belongsTo(Dic::class, "direction_id", "key")->where(function ($q)
            {
            $q->where("code", 'ProcessDirection');
            });
        }
    public function pro_dept()
        {
        return $this->belongsTo(ProDept::class);
        }
    public function out_pro_dept()
        {
        return $this->belongsTo(ProDept::class);
        }
    public function outfactory()
        {
        return $this->belongsTo(Company::class);
        }
    public function out_outfactory()
        {
        return $this->belongsTo(Company::class);
        }
    public function getTargetAttribute()
        {
        return isEmpty($this->target_model) ? null :
            AppCall::make($this->target_model)->whereId($this->target_value)->first();
        }
    public function _created($model)
        {
        if ($model->process->name == "倒卷")
            $model->open_card->updateRoll_quantity();

        if ($model->process->name == "复合结束" && $model->faultle_quantity > 0)
            $model->open_card->updateComposite_quantity($model);
        }
    public function _deleted($model)
        {
        if ($model->process->name == "倒卷")
            $model->open_card->updateRoll_quantity();

        if ($model->process->name == "复合结束" && $model->faultle_quantity > 0)
            $model->open_card->updateComposite_quantity($model);
        }
    }
